﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace FormatXml
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] args = Environment.GetCommandLineArgs();

            //foreach (string arg in args)
            //{
            //    richTextBox1.AppendText("arg: " + arg + Environment.NewLine);
            //}

            if (args.Length > 1 && File.Exists(args[1]))
            {
                ProcessFile(args[1]);
            }
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = ((string[])e.Data.GetData(DataFormats.FileDrop));
            bool first = true;
            foreach (string file in files)
            {
                if (first)
                {
                    ProcessFile(file);
                }
                first = false;
            }
        }

        string m_CurrentFile;
        private void ProcessFile(string filePath)
        {
            try
            {
                m_CurrentFile = filePath;

                string filename = Path.GetFileName(filePath);
                this.Text = "XML Formatter - " + filename;
                saveToolStripMenuItem.Enabled = true;
                saveAsToolStripMenuItem.Enabled = true;
                saveToolStripMenuItem.Text = "&Save " + filename;
                saveAsToolStripMenuItem.Text = "Save " + filename + " &As...";

                string Xml = File.ReadAllText(filePath);
                Xml = FormatXml(Xml);
                richTextBox1.Text = Xml;
            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error: " + ex.Message);
            }
        }

        static string FormatXml(string sUnformattedXml)
        {
            //load unformatted xml into a dom
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(sUnformattedXml);

            //will hold formatted xml
            StringBuilder sb = new StringBuilder();

            //pumps the formatted xml into the StringBuilder above
            StringWriter sw = new StringWriter(sb);

            //does the formatting
            XmlTextWriter xtw = null;

            try
            {
                //point the xtw at the StringWriter
                xtw = new XmlTextWriter(sw);

                //we want the output formatted
                xtw.Formatting = Formatting.Indented;

                //get the dom to dump its contents into the xtw 
                xd.WriteTo(xtw);
            }
            finally
            {
                //clean up even if error
                if (xtw != null)
                    xtw.Close();
            }

            //return the formatted xml
            return sb.ToString().Replace(" />", "/>");
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File.WriteAllText(m_CurrentFile, richTextBox1.Text);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = Path.GetDirectoryName(m_CurrentFile);
            saveFileDialog1.FileName = Path.GetFileName(m_CurrentFile);
            DialogResult result = saveFileDialog1.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutXMLFormatterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (FormAbout aboutDialog = new FormAbout())
            {
                aboutDialog.ShowDialog(this);
            }
        }
    }
}
